﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Jungle_Timers.Controls;
using Jungle_Timers.Entities;
using Jungle_Timers.Helpers;
using Jungle_Timers.Wrappers;

namespace Jungle_Timers
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _isexpanded;
        private MainWindowViewModel _vm;

        public MainWindow()
        {
            InitializeComponent();

            _vm = new MainWindowViewModel();

            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dt.Tick += new EventHandler(EnsureWindowOnTop);
            dt.Start();

            this.DataContext = _vm;
        }
        
        void EnsureWindowOnTop(object sender, EventArgs e)
        {
            Win32.ShowInactiveTopmost(this);
        }
        
      
        private void ActiveTimer_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ActiveTimerItem at = (sender as ActiveTimer).DataContext as ActiveTimerItem;
            _vm.RemoveTimer(at);
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            source.AddHook(new HwndSourceHook(WndProc));  
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_MOUSEACTIVATE = 0x0021;
            const int MA_NOACTIVATE = 3;

            if (msg == WM_MOUSEACTIVATE)
            {
                handled = true;
                return new IntPtr(MA_NOACTIVATE);
            }

            return IntPtr.Zero;
        }
        
        private void GolemButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.AddTimer(TimerNames.Golem);
        }

        private void LizardButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.AddTimer(TimerNames.Lizard);
        }

        private void DragonButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.AddTimer(TimerNames.Dragon);
        }

        private void BaronButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.AddTimer(TimerNames.Baron);
        }

        private void CloseButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.FocusLoL();
            this.Close();
        }

        private void ExpandButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.FocusLoL();
            TimerPanel.Visibility = Visibility.Visible;
            CollapseButton.Visibility = System.Windows.Visibility.Visible;
            ExpandButton.Visibility = System.Windows.Visibility.Collapsed;

        }

        private void CollapseButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            _vm.FocusLoL();
            TimerPanel.Visibility = Visibility.Collapsed;
            CollapseButton.Visibility = System.Windows.Visibility.Collapsed;
            ExpandButton.Visibility = System.Windows.Visibility.Visible;

        }

        private void DragButton_PreviewLeftMouseDown(object sender, MouseButtonEventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            Win32.ReleaseCapture();
            Win32.SendMessage(handle, Win32.WM_NCLBUTTONDOWN, Win32.HT_CAPTION, 0);
        }

        private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            _vm.FocusLoL();
            
        }  

        
     
    }

    public class MainWindowViewModel : DependencyObject
    {

        public TimerManager TimerManager { get; set; }

        public MainWindowViewModel()
        {
            TimerManager = new TimerManager();
        }

        public void AddTimer(TimerNames timerName)
        {
            FocusLoL();
            ActiveTimerItem ac = new ActiveTimerItem(timerName);
            TimerManager.Add(ac);
        }

        internal void RemoveTimer(ActiveTimerItem activeTimerItem)
        {
            FocusLoL();
            TimerManager.Remove(activeTimerItem);
        }

        public void FocusLoL()
        {
            IntPtr lolWnd= Win32.FindWindow(null, "League of Legends (TM) Client");
            Win32.ShowActiveTopmost(lolWnd);
        }
    }

}
